package com.ddlx.test.binder.aidl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.RemoteException;
import android.renderscript.Element;
import android.util.Log;

public class DataSender extends IDataSender.Stub{

	private String tag;
	private Context context;
	public DataSender( String tag, Context context ) {
		this.tag = tag;
		this.context = context;
	}
	
	private void logD( String info) {
		Log.d("ddlx", "["+tag+"]"+info);
	}
	
	private void logV( String info) {
		Log.v("ddlx", "["+tag+"]"+info);
	}
	
	private void logE( String info) {
		Log.e("ddlx", "["+tag+"]"+info);
	}
	
	private void logi( String info) {
		Log.i("ddlx", "["+tag+"]"+info);
	}
	@Override
	public Result sendData(Message msg) throws RemoteException {
		Result result = new Result();
		result.setResId(Result.ERR_OK);
		result.setInfo("一切正常，欧了");
		logD("[sendData] msg:"+msg+" result:"+result);
		return result ;
	}

	@Override
	public void postData(Message msg, IPostCallback callback)
			throws RemoteException {
		Result result = new Result();
		result.setResId(Result.ERR_OK);
		result.setInfo("一切正常，欧了");
		logD("[postData] msg:"+msg+" callback:"+callback+" result:"+result);
		if ( callback != null  ) {
			callback.onResult(result);
		}
	}

	@Override
	public Result sendFD(Message msg) throws RemoteException {
		FileDescriptor fileDescriptor = msg.getFdData();
		String readFileData = null;
		String readFDData = null;
		Result result = new Result();
		File file = new File( "/data/data/com.ddlx.test.binder/files/binder/test.txt");
		BufferedReader reader = null;
		try {
			reader = new BufferedReader( new FileReader(file));
			readFileData = reader.readLine();
		} catch (FileNotFoundException e1) {
//			result.setResId(Result.ERR_ERROR);
//			result.setInfo("!!!找不到文件!!!");
			// TODO Auto-generated catch block
			readFileData = e1.toString();
			e1.printStackTrace();
		} catch (IOException e) {
			readFileData = e.toString();
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			if ( reader != null ) {

				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		if ( fileDescriptor != null ) {
			reader = new BufferedReader( new FileReader(fileDescriptor));
			try {
				readFDData = reader.readLine();
			} catch (IOException e) {
				readFDData = e.toString();
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {

				if ( reader != null ) {

					try {
						reader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		if ( readFDData == null ) {
			result.setResId(Result.ERR_ERROR);
		}
		result.setInfo("文件路径为:"+file.getAbsolutePath()+"\n直接打开文件读取="+readFileData+"\n通过FD读取的内容="+readFDData);
		logD("[sendData] msg:"+msg+" result:"+result);
		return result;
	}

	@Override
	public void triggerException(String exceptionClassName) throws RemoteException {
		logD("[triggerException] exceptionClassName:"+exceptionClassName);
		Object object = null;
		try {
			Class<?> cls = Class.forName(exceptionClassName);
			Constructor<?> cons = null;
			do {
				try {
					cons = cls.getConstructor();
					if ( cons != null ) {
						try {
							object = cons.newInstance();
							break;
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InstantiationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					cons = cls.getConstructor(String.class);
					if ( cons != null ) {
						try {
							object = cons.newInstance("ddlx test");
							break;
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InstantiationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} while(false);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if ( object instanceof RuntimeException ) {
			throw (RuntimeException)object;
		} else if(object instanceof Error){
			throw (Error)object;
		} else {
			//
		}
	}

	@Override
	public void sleep(long ms) throws RemoteException {
		logD("[sleep] ms:"+ms);
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logD("[sleep] ms:"+ms+" sleep ok.");
	}

	@Override
	public void asyncSleep(long ms) throws RemoteException {
		logD("[asyncSleep] ms:"+ms);
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logD("[asyncSleep] ms:"+ms+" sleep ok.");
	}

	@Override
	public void kill(int exitId) throws RemoteException {
		logD("[kill] exitId:"+exitId);
		System.exit(exitId);
	}

}
